package com.race.realm;

import com.race.common.constant.ResponseEnums;
import com.race.common.returntype.R;
import com.race.entities.po.Permission;
import com.race.entities.po.Role;
import com.race.entities.po.User;
import com.race.service.UserService;
import com.race.service.impl.UserServiceImpl;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClaissName: CustomRealm
 * @Description: TODO
 * @Author: xcheng
 */
public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserServiceImpl userService;
    /**
     * 获取授权信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("————权限认证————");
        String username = (String) principalCollection.getPrimaryPrincipal();
        //调用service拿到user对象
        R<User> r = userService.findAllUserInfoByUsername(username);
        if (!(ResponseEnums.SUCCESS.getCode().equals(r.getCode()))){
            return null;
        }
        User u = r.getData();
        List<String> stringRoleList = new ArrayList<>();
        List<String> stringPermissionList = new ArrayList<>();

        for (Role role : u.getRoles()){
                stringRoleList.add(role.getName());
            for (Permission permission : role.getPermissions()){
                if (permission!=null) {
                    stringPermissionList.add(permission.getName());
                }
            }
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRoles(stringRoleList);
        simpleAuthorizationInfo.addStringPermissions(stringPermissionList);
        return simpleAuthorizationInfo;
    }

    /**
     * 获取身份验证信息
     * Shiro中，最终是通过 Realm 来获取应用程序中的用户、角色及权限信息的。
     *
     * @param token 用户身份信息 token
     * @return 返回封装了用户信息的 AuthenticationInfo 实例
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("————身份认证方法————");
        //从token中获取用户名
        String username = (String) token.getPrincipal();
        R<User> r = userService.findAllUserInfoByUsername(username);
        if (!(ResponseEnums.SUCCESS.getCode().equals(r.getCode()))){
            return null;
        }
        User u = r.getData();
        String pwd = u.getPassword();
        if(pwd == null || "".equals(pwd)){
            return null;
        }
        return new SimpleAuthenticationInfo(username,u.getPassword(),this.getClass().getName());
    }
}
